﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace Entities
{
    [Serializable()] 
    public class ServerConfig
    {
        private String _ipServer;
        private String _dbName;
        private String _user;
        private String _pwd;

        public String ipServer 
        { 
            set
            {
                _ipServer = System.Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(value));
            }
            get
            {
                return System.Text.ASCIIEncoding.ASCII.GetString(System.Convert.FromBase64String(_ipServer));
            } 
        }
        public String dbName 
        { 
            set
            {
                _dbName = System.Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(value));
            }
            get
            {
                return System.Text.ASCIIEncoding.ASCII.GetString(System.Convert.FromBase64String(_dbName));
            }
        }
        public String user { 
            set
            {
                _user = System.Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(value));
            }
            get 
            {
                return System.Text.ASCIIEncoding.ASCII.GetString(System.Convert.FromBase64String(_user));
            } 
        }
        public String pwd 
        { 
            set
            {
                _pwd = System.Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(value));
            }
            get
            {
                return System.Text.ASCIIEncoding.ASCII.GetString(System.Convert.FromBase64String(_pwd));
            }
        }       

        public ServerConfig(String ipServer, String dbName, String user, String pwd)
        {
            this.ipServer = ipServer;
            this.dbName = dbName;
            this.user = user;
            this.pwd = pwd;
        }

        //Deserialization constructor.
        public ServerConfig(SerializationInfo info, StreamingContext ctxt)
        {
            //Get the values from info and assign them to the appropriate properties
            ipServer = (String)info.GetValue("ipServer", typeof(string));
            dbName = (String)info.GetValue("dbName", typeof(string));
            user = (String)info.GetValue("user", typeof(string));
            pwd = (String)info.GetValue("pwd", typeof(string));
        }
        
        //Serialization function.
        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            //You can use any custom name for your name-value pair. But make sure you
            // read the values with the same name. For ex:- If you write EmpId as "EmployeeId"
            // then you should read the same with "EmployeeId"
            info.AddValue("ipServer", ipServer);
            info.AddValue("dbName", dbName);
            info.AddValue("user", user);
            info.AddValue("pwd", pwd);
        }

        public ServerConfig()
        {

        }
    }
}
